package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.EnCryptDeCrypt;
import com.longrise.bjjt.util.PrefUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * Created by Administrator on 2016/5/17.
 */
public class ModifyPwdView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private EditText oldPwdET;

    private EditText newPwdET;

    private EditText cnewPwdET;

    private Button confirmBtn;

    private BaseApplication application;

    private ImageView opwdIV;

    private ImageView npwdIV;

    private ImageView cpwdIV;

    public ModifyPwdView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = View.inflate(mContext, R.layout.view_modifypwd, null);
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText(R.string.modify_pwd);
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        oldPwdET = (EditText) mView.findViewById(R.id.mpwd_et_oldpwd);
        opwdIV = (ImageView) mView.findViewById(R.id.modify_iv_opwd);
        newPwdET = (EditText) mView.findViewById(R.id.mpwd_et_newpwd);
        npwdIV = (ImageView) mView.findViewById(R.id.modify_iv_npwd);
        cnewPwdET = (EditText) mView.findViewById(R.id.mpwd_et_cnewpwd);
        cpwdIV = (ImageView) mView.findViewById(R.id.modify_iv_cpwd);
        confirmBtn = (Button) mView.findViewById(R.id.mpwd_btn_confirm);

        setListener(true);
        application = (BaseApplication) mContext.getApplicationContext();

        oldPwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
        newPwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
        cnewPwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    private void setListener(boolean flag) {
        if (confirmBtn != null) {
            confirmBtn.setOnClickListener(flag ? this : null);
        }
        if (opwdIV != null) {
            opwdIV.setOnClickListener(flag ? this : null);
        }
        if (npwdIV != null) {
            npwdIV.setOnClickListener(flag ? this : null);
        }
        if (cpwdIV != null) {
            cpwdIV.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.mpwd_btn_confirm://修改密码
                    modifyPwd();
                    break;
                case R.id.modify_iv_opwd://旧密码
                    CommontUtils.setSeePwd(oldPwdET);
                    setShowDrawable(oldPwdET, opwdIV);
                    break;
                case R.id.modify_iv_npwd://新密码
                    CommontUtils.setSeePwd(newPwdET);
                    setShowDrawable(newPwdET, npwdIV);
                    break;
                case R.id.modify_iv_cpwd://重复密码
                    CommontUtils.setSeePwd(cnewPwdET);
                    setShowDrawable(cnewPwdET, cpwdIV);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //设置明文密码时的图像标识
    private void setShowDrawable(EditText editText, ImageView imageView) {
        TransformationMethod method = editText.getTransformationMethod();
        if (method instanceof PasswordTransformationMethod) {
            imageView.setImageResource(R.mipmap.eye);
        } else {
            imageView.setImageResource(R.mipmap.eye_select);
        }
    }

    /**
     * 修改密码
     *
     * @throws Exception
     */
    private void modifyPwd() throws Exception {
        String oldPwd = oldPwdET.getText().toString().trim();
        if (TextUtils.isEmpty(oldPwd)) {
            UiUtil.showToast(mContext, "请输入正确的当前密码");
            return;
        }
        String saveOld = PrefUtils.getString(mContext, "loginPassword", null);
        if (!TextUtils.equals(oldPwd, saveOld)) {
            UiUtil.showToast(mContext, "当前密码输入不正确");
            return;
        }
        final String newPwd = newPwdET.getText().toString().trim();
        if (!CommontUtils.isPassword(newPwd)) {
            UiUtil.showToast(mContext, "请输入6~15位由字母和数字组合的密码！");
            return;
        }
//        String cnewPwd = cnewPwdET.getText().toString().trim();
//        if (TextUtils.isEmpty(cnewPwd)) {
//            UiUtil.showToast(mContext, "请再输入正确的新密码");
//            return;
//        }
//        if (!TextUtils.equals(newPwd, cnewPwd)) {
//            UiUtil.showToast(mContext, "两次输入的新密码不一致");
//            return;
//        }
        String userflag = application.getKckpName();
        String oldPwdEn = application.getKckpPass();
        String token = application.getToken();
        final String newPwdEn = EnCryptDeCrypt.getInstance().EnCrypt(newPwd);
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).modifyPwd(userflag, oldPwdEn, newPwdEn, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(getContext(), error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            UiUtil.showToast(getContext(), redes);
                        }
                        String restate = bean.getString("restate");
                        if (TextUtils.equals("1", restate)) {
                            //重新设置密码的值
//                            PrefUtils.setString(mContext, "loginPassword", newPwd);
//                            application.setKckpPass(newPwdEn);
//                            OnDestroy();
//                            closeForm();

                            // 清空sp的账号，密码，token
                            PrefUtils.removeStringData(mContext, "loginInfo");
                            PrefUtils.removeStringData(mContext, "loginUserName");
                            PrefUtils.removeStringData(mContext, "loginPassword");
                            //清除缓存
                            application.setLoginInfo(null);
                            application.setToken(null);
                            UiUtil.showToast(mContext, "修改成功，请重新登录！");
                            //回到登录页
                            if (FrameworkManager.getInstance().isPopForm()) {
                                FrameworkManager.getInstance().closePopForm();
                            }
                            FrameworkManager.getInstance().destroyAllForm();
                        }
                    }
                });
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        setListener(false);
        DialogUtils.destroyDialog();
    }
}
